/****************************************************************************
 * Copyright (c) 2009 Remy Chi Jian Suen and others.
 *
 * This program and the accompanying materials are made
 * available under the terms of the Eclipse Public License 2.0
 * which is available at https://www.eclipse.org/legal/epl-2.0/
 *
 * Contributors:
 *     Remy Chi Jian Suen - initial API and implementation
 *
 * SPDX-License-Identifier: EPL-2.0
 *****************************************************************************/
package org.eclipse.team.internal.ecf.ui;

import org.eclipse.ecf.core.identity.ID;
import org.eclipse.ecf.core.util.ECFException;
import org.eclipse.ecf.datashare.IChannelContainerAdapter;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.osgi.util.NLS;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;
import org.eclipse.team.internal.ecf.core.RemoteShare;
import org.eclipse.ui.IWorkbenchWindow;
import org.eclipse.ui.PlatformUI;

public class WorkbenchAwareRemoteShare extends RemoteShare {

	WorkbenchAwareRemoteShare(IChannelContainerAdapter adapter) throws ECFException {
		super(adapter);
	}

	protected boolean prompt(final ID fromId, String[] paths) {
		IWorkbenchWindow[] windows = PlatformUI.getWorkbench().getWorkbenchWindows();
		for (int i = 0; i < windows.length; i++) {
			final Shell shell = windows[i].getShell();
			if (shell != null && !shell.isDisposed()) {
				final boolean[] prompt = {false};

				final StringBuffer buffer = new StringBuffer(Text.DELIMITER);
				for (int j = 0; j < paths.length; j++) {
					buffer.append(paths[j]).append(Text.DELIMITER);
				}

				PlatformUI.getWorkbench().getDisplay().syncExec(new Runnable() {
					public void run() {
						prompt[0] = MessageDialog.openConfirm(shell, null, NLS.bind(Messages.WorkbenchAwareRemoteShare_PromptMessage, fromId.getName(), buffer.toString()));
					}
				});

				return prompt[0];
			}
		}
		return false;
	}
}
